package com.lanxin.oa.approve.controller;
import com.lanxin.bean.LcFlow;
import com.lanxin.oa.approve.service.ApproveService;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/approve")
public class ApproveController {

    @Autowired
    private ApproveService approveService;

    @RequestMapping("/selectApprove")
    public ModelAndView selectApprove(Integer page, Integer pageSize, LcFlow flow,String docType1){
        PageBean<LcFlow> pageBean = approveService.getApprovePage(page, pageSize,flow,docType1);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageBean", pageBean);
        mv.addObject("flow",flow);
        mv.addObject("docType1",docType1);
        mv.setViewName("../view/approve/listapprove.jsp");
        return mv;
    }

    @RequestMapping("/editFlowState")
    @ResponseBody
    public boolean editFlowState(Integer  oid,String state){

        return  approveService.updateApproveStatus(oid,state);
    }

    @RequestMapping("/checkFlowName")
    @ResponseBody
    public Map<String,Boolean> checkFlowName(String flowName, Integer oid){
        Map<String,Boolean> map=new HashMap<String, Boolean>();
        boolean b=approveService.selectApproveByName(flowName,oid);
        if(!b){
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("/deleteFlow")
    @ResponseBody
    public boolean deleteFlow(Integer  oid){
        return approveService.deleteApprove(oid);
    }

    @RequestMapping("/editFlow")
    public ModelAndView  editFlow(LcFlow flow){
        approveService.editApprove(flow);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:/approve/selectApprove");
        return mv;
    }
}
